#!/bin/bash

ppp_user=$1
password=$2
eth=$3

[ -z "$ppp_user" ]&&{
echo "没有指定拨号账号..."
echo "示例: $0 user password ethx"
exit 0
}

[ -z "$password" ]&&{
echo "没有指定拨号密码..."
echo "示例: $0 user password ethx"
exit 0
}

[ -z "$eth" ]&&{
echo "没有指定拨号网卡..."
echo "示例: $0 user password ethx"
exit 0
}

echo "请等待,正在进行配置..."

ppps=`ps -ef|grep ppp0|grep -v "grep"|wc -l`
if [ $ppps -gt 0 ];then
    ifdown ppp0;
fi


if [ -d "/etc/sysconfig/network-scripts/ifcfg-ppp0" ];then
    echo "" > /etc/sysconfig/network-scripts/ifcfg-ppp0
else
    touch /etc/sysconfig/network-scripts/ifcfg-ppp0
fi

echo "USERCTL=yes" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "BOOTPROTO=dialup" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "NAME=DSLppp0" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "DEVICE=ppp0" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "TYPE=xDSL" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "PIDFILE=/var/run/pppoe-adsl.pid" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "FIREWALL=NONE" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "PING=." >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "PPPOE_TIMEOUT=80" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "LCP_FAILURE=3" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "LCP_INTERVAL=20" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "CLAMPMSS=1412" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "CONNECT_POLL=6" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "CONNECT_TIMEOUT=60" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "DEFROUTE=no" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "SYNCHRONOUS=no" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "ETH=$eth" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "PROVIDER=DSLppp0" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "USER=$ppp_user" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "PEERDNS=no" >> /etc/sysconfig/network-scripts/ifcfg-ppp0
echo "DEMAND=no" >> /etc/sysconfig/network-scripts/ifcfg-ppp0


line1=`cat /etc/ppp/chap-secrets|grep "*"|wc -l`

if [ $line1 -lt 1 ];then
    echo "\"$ppp_user\"    *    \"$password\"" >  /etc/ppp/chap-secrets
else
    user=`cat /etc/ppp/chap-secrets|grep "*"|awk '{print$1}'`
    passwd=`cat /etc/ppp/chap-secrets|grep "*"|awk '{print$3}'`
    sed -i "s/$user/\"$ppp_user\"/g" /etc/ppp/chap-secrets
    sed -i "s/$passwd/\"$password\"/g" /etc/ppp/chap-secrets
fi

line2=`cat /etc/ppp/pap-secrets|grep "*"|wc -l`

if [ $line1 -lt 1 ];then
        echo "\"$ppp_user\"     *       \"$password\"" >> /etc/ppp/pap-secrets
else
        user=`cat /etc/ppp/pap-secrets|grep "*"|awk '{print$1}'`
        passwd=`cat /etc/ppp/pap-secrets|grep "*"|awk '{print$3}'`
        sed -i "s/$user/\"$ppp_user\"/g" /etc/ppp/pap-secrets
        sed -i "s/$passwd/\"$password\"/g" /etc/ppp/pap-secrets
fi


ck=`cat /sbin/ifup|grep "routeok"|wc -l`

if [ $ck -lt 1 ];then
        sed -i "s/exec \${OTHERSCRIPT} \${CONFIG} \$2//g" /sbin/ifup

        echo "#route" >> /sbin/ifup
        echo "{" >> /sbin/ifup
        echo "sleep 10" >> /sbin/ifup
        echo "route_ip=\`/sbin/pppoe-status|grep \"inet\"|awk '{print\$4}'|awk -F\/ '{print\$1}'\`" >> /sbin/ifup
        echo "len=\`echo \$route_ip|wc -c\`" >> /sbin/ifup
        echo "if [ \$len -gt 10 ];then" >> /sbin/ifup
        echo "        route add default gw \$route_ip dev ppp0" >> /sbin/ifup
        echo "fi" >> /sbin/ifup
        echo "}&" >> /sbin/ifup
        echo "" >> /sbin/ifup
        echo "exec \${OTHERSCRIPT} \${CONFIG} \$2" >> /sbin/ifup
        echo "" >> /sbin/ifup
        echo "#routeok" >> /sbin/ifup
fi

ifup ppp0
if [ $? -eq 0 ];then
    echo "ifup ok"
else
    echo "配置失败,请重新配置，比如重新选择网卡或者确认账号密码？"
fi


# 执行：./ppp_auto.sh sz20012231@163.gd 123456 enp1s0 即可自动配置
# 第一个参数是账号，第二个是密码，第三个是以太网卡代号